package net.zhengxinyun.performance.service.departClass;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.entity.CountryUnitIndicatorsEntity;
import net.zhengxinyun.performance.entity.DepartClassHead;
import net.zhengxinyun.performance.mapper.departClass.DepartClassIHeadMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.DataFormatException;

@Service
@Transactional
public class DepartHeadServiceImpl implements  DepartHeadService {

    @Autowired
    private DepartClassIHeadMapper mapper;

    @Override
    public List<DepartClassHead> queryItem(DepartClassHead entity) {
        return mapper.queryItem(entity);
    }

    @Override
    public List<DepartClassHead> queryByDatetime(String datetime) {
        return mapper.queryByDatetime(datetime);
    }

    @Override
    public int update(DepartClassHead entity) {
        return mapper.update(entity);
    }

    @Override
    public int delete(Long id) {
        return mapper.delete(id);
    }

    @Override
    public int saveItems(List<DepartClassHead> list) {
        return mapper.saveItems(list);
    }

    @Override
    public DepartClassHead queryByIndexId(Long indexId) {
        return mapper.queryByIndexId(indexId);
    }


    @Override
    public int insertSingle(DepartClassHead entity) {
        return mapper.insertSingle(entity);
    }

    @Override
    public int deleteByIndexId(Long indexId) {
        return mapper.deleteByIndexId(indexId);
    }

    @Override
    public int updateByIndexId(DepartClassHead entity) {
        return mapper.updateByIndexId(entity);
    }

    //更新表头；
    @Override
    public int updateDepartHead(List<CountryUnitIndicatorsEntity> list) {
        List<DepartClassHead> saveList = new ArrayList<>();
        List<DepartClassHead> updateList  = new ArrayList<>();

        for (CountryUnitIndicatorsEntity entity : list) {

            DepartClassHead head = queryByIndexId(entity.getId());
            if(ObjectUtils.isNotBlank(head))
            {
                updateList.add(new DepartClassHead(entity.getIndexName(),entity.getDateTime(),entity.getId()));
            }else {
                saveList.add(new DepartClassHead(entity.getIndexName(),entity.getDateTime(),entity.getId()));
            }

        }
        if(saveList.size()>0)
        {
            saveItems(saveList);
        }

        for (DepartClassHead head : updateList) {
            updateByIndexId(head);
        }


        return 0;
    }
}
